package gorm.springboot.demo.services

import gorm.springboot.demo.model.Person
import org.springframework.stereotype.Service

@Service
class PersonServiceImp implements PersonService {

    @Override
    List<Person> findAll() {
        return Person.findAll()
    }

    @Override
    Person findById(long id) {
        return Person.findById(id)
    }

    @Override
    void save(Person person) {
        person.save()

    }

    @Override
    def list(int offset, int max) {
        return Person.list(offset: offset, max: max, sort: "id", order: "desc")
    }

    @Override
    Person search(Map map) {

        return Person.findWhere(map)
    }

    /**
     * Conjunction 联合
     * Disjunction 分离
     * Negation 否定
     * As mentioned previously you can combine regular Groovy logical operators (|| and &&) to form conjunctions and disjunctions:
     */

    /**
     * def c = Account.createCriteria()
     * def results = c {*     between("balance", 500, 1000)
     *     eq("branch", "London")
     *     or {*         like("holderFirstName", "Fred%")
     *         like("holderFirstName", "Barney%")
     *}*     maxResults(10)
     *     order("holderLastName", "desc")
     *}*/
    def criteria0() {
        def criteria = Person.createCriteria()
        def result = criteria.list {

            eq("lastname", "w")
        }
    }

    def criteria1() {
        def criteria = Person.createCriteria()
        def result = criteria.list {
            eq('firstName', 'w')
            order('id', 'asc')

        }

    }

    def criteria2() {
        def criteria = Person.createCriteria()
        criteria.between("id", 1, 100)
                .like("lastname", "www")

    }
}
